package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.MemberReservationDto;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.TimestampCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @version: 1.0
 * @Author: ljy
 * @description:
 * @date: 2025-07-31 20:38
 */

@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService {

    @Autowired
    private MemberReservationMapper memberReservationMapper;

    @Override
    public boolean saveReservation(MemberReservationDto memberReservationDto) {
        //属性拷贝
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(memberReservationDto, reservation);
        //调用方法
        boolean flag = save(reservation);
        return flag;
    }

    @Override
    public List<Reservation> selectMemberReservationList(Integer status) {
        if (status == null) {
            status = 0;
        }
        return list(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, status));
    }

    @Override
    public List<TimestampCountVo> countByTime(long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        TimestampCountVo timestampCountVo = memberReservationMapper.countByTime(startTime, endTime);
        if (ObjectUtil.isNotNull(timestampCountVo)) {
            return List.of(timestampCountVo);
        }
        return List.of(new TimestampCountVo(LocalDateTime.now(), 999));
    }

    @Override
    public Long getCancelledReservationCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Long userId = UserThreadLocal.getUserId();
        long count = count(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 2)
                .eq(Reservation::getUpdateBy, userId)
                .between(Reservation::getUpdateTime, startTime, endTime));
        return count;
    }

    @Override
    public boolean cancelReservation(Long id) {
        //封装对象
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setStatus(2);
        return updateById(reservation);
    }
}
